home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
EnigmA Amiga Run 1996 February
/
EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso
/
earcd
/
util4
/
autoftp.lha
/
AutoFTP.rexx
Wrap
OS/2 REXX Batch file
|
1996-01-12
|
16KB
|
478 lines
/*****************************************************************************/
/* */
/* AutoFTP.rexx : an ARexx script to automatically a list of files from an */
/* Aminet archive site. When run, the new RECENT listing of */
/* the site is downloaded, and GadMGet is run on that */
/* listing. After GadMGet is terminated, GadMGet's output is */
/* sent to ncFTP, which automatically connects to the site */
/* and downloads the programs specified by the user. */
/* */
/* version: 1.5 */
/* */
/* Don't use AutoFTP unless you have AmiTCP, GadMGet, and ncFTP already */
/* working correctly. AutoFTP relies on these programs to work. */
/* */
/* */
/* Command line Switches accepted : */
/* YES -- Automatically download a new RECENT file */
/* NO -- Don't download a RECENT file */
/* DEBUG -- Write all AmigaDOS commands executed to stdout */
/* */
/* If neither of the above is specified, a requester will be displayed */
/* asking the user whether or not to get a new RECENT file or not. */
/* */
/*****************************************************************************/
/**************************************************************************/
/* ---------- ADJUST THESE PARAMETERS TO FIT YOUR PREFERENCES ----------- */
/**************************************************************************/
/* The Aminet FTP Site you prefer to use */
AminetSite = "ftp.netnet.net"
/* The root directory of Aminet within that site */
AminetRootDir = "/pub/aminet"
/* The directory you want your copy of RECENT to be in */
LocalRecentDir = "dh0:"
/* Path & Filename of GadMGet executable */
GadMGet = "GadMget"
/* Path & Filename of ncFTP executable */
ncFTP = "amitcp:bin/ncFTP"
/* Any miscellaneous startup arguments you wish to be included
on GadMGet's command line call. See GadMGet documentation on
"Startup Options" for a list of possible options. */
GadMGetArgs = ""
/* The directory path you want AutoFTP to download files to. */
DownloadDir = "ram:"
/* Do you want AutoFTP to keep saved selected lines from previous
GadMget sessions? */
PreserveSelected = YES
/* Do you want AutoFTP to remove lines from the new RECENT file that you
already have selected in the old RECENT file? (This option is only
significant if PreserveSelected is YES) */
DoPruning = YES
/* Do you want AutoFTP to add the comment lines from the RECENT file
to the downloaded files, as FileNotes? */
AddFileNotes = YES
/* Template string for FileNotes. $GADMGETDIR, $GADMGETFILE, and
$GADMGETCOMMENT are avaiable keywords that will be replaced by
their respective values in the FileNotes. */
FileNoteFormatString = "$GADMGETDIR $GADMGETFILE [$GADMGETCOMMENT]"
/* If set to non-zero, each of these will ensure that its related
field in the FileNotes is the length specified. */
GadMgetDirLength = 13
GadMgetFileLength = 18
GadMgetCommLength = 40
/**************************************************************************/
/* ------- END USER PARAMETERS --- DO NOT MODIFY BELOW THIS LINE -------- */
/**************************************************************************/
/**************************************************************************/
/* ---------------------- BEGIN AUTOFTP SCRIPT CODE --------------------- */
/**************************************************************************/
options results
parse arg CommandLineArg
DownloadRECENT = MAYBE
Debug = FALSE
CommandLineArg = upper(CommandLineArg)
if (CommandLineArg == "?") then do
say "AutoFTP: AutoFTP YES/S NO/S DEBUG/S"
exit
end
if (CommandLineArg == "YES") then DownloadRECENT = YES
if (CommandLineArg == "NO") then DownloadRECENT = NO
if (CommandLineArg == "DEBUG") then do
say "AutoFTP: Debug mode on."
Debug = TRUE
end
if (DownloadRECENT == MAYBE) then do
/* Load rexxreqtools.library if possible */
if (show('L','rexxreqtools.library')) then RexxReqToolsLoaded = 1
else RexxReqToolsLoaded = addlib('rexxreqtools.library',0,-30)
if (RexxReqToolsLoaded) then UserResponse = rtezrequest('Shall I download the new RECENT file now?',"_Yes|_No")
else do
say "[Warning: rexxreqtools.library not available!]"
say "Shall I download a new RECENT file now? (y/n)"
pull UserResponse
UserString = upper(UserResponse)
UserResponse = 1
if (left(UserString,1) == "N") then UserResponse = 0
end
if (UserResponse == 1) then DownloadRECENT = YES
else DownloadRECENT = NO
end
/* Fix up the syntax of some parameters */
if (right(AminetSite,1) ~= ":") then AminetSite = AminetSite || ":"
if (right(AminetRootDir,1) ~= "/") then AminetRootDir = AminetRootDir || "/"
/* Get a nice temporary filename or two */
ProcessID = time('S')
TempFile = "t:AutoFTP.temp." || ProcessID
BackupFile = "t:AutoFTP.script.bak"
TempRecent = "t:AutoFTP.RECENT." || ProcessID
GadMGetArgs = GadMGetArgs || " NOSIMPLEPATHS AMINETPATH " || AminetRootDir || " OUTPUT " || TempFile
LocalRecentFile = LocalRecentDir || "RECENT"
if (DownloadRECENT == YES) then do
/* Construct request for RECENT */
RECENTfile = AminetSite || AminetRootDir || "RECENT"
say "Retrieving the current RECENT file from " || AminetSite
GetRECENTCommand = ncFTP || " -r " || RECENTfile
/* Get RECENT file */
call Pragma('D',"t:")
success = DoCommand(GetRECENTCommand)
RenameCommand = "rename RECENT " || TempRecent || " QUIET"
success = DoCommand(RenameCommand)
call Pragma('D',LocalRECENTdir)
/* Now either process the downloaded RECENT file to save selected lines,
or if that's turned off, just copy it over. */
if (PreserveSelected = YES) then do
success = SmartCopy(LocalRecentFile,TempRecent,LocalRecentFile)
if (success == 0) then do
say "WARNING: File Merge failed. Falling back to simple copy"
CopyCommand = "copy " || TempRecent || " " || LocalRecentFile || " QUIET"
success = doCommand(CopyCommand)
end
end
else do
CopyCommand = "copy " || TempRecent || " " || LocalRecentFile || " QUIET"
success = doCommand(CopyCommand)
end
DeleteTempRecentCommand = "delete " || TempRecent || " QUIET"
success = DoCommand(DeleteTempRecentCommand)
end
/* Now we run GadMGet */
GadMGetCommand = GadMGet || " " || LocalRecentFile || " " || GadMGetArgs
success = DoCommand(GadMGetCommand)
/* Make sure that the output file is there */
if (~Exists(TempFile)) then do
say "You didn't select any files. Exiting..."
exit
end
/* Make a backup copy of GadMGet's output, that way if the ftp session
breaks, at least the user still has the last output to use manually
is s/he wants. */
BackupCommand = 'copy ' || TempFile || " " || BackupFile || " QUIET"
success = DoCommand(BackupCommand)
/* Let the user see the list... */
say "------FTP script is as follows-----"
address COMMAND "type " TempFile
say "-----------End FTP script----------"
/* Get the current time, in case we need it for later */
/* Change to the directory the user wishes to download to */
call Pragma('D',DownloadDir)
DownloadCommand = ncFTP || " -r " || AminetSite || AminetRootDir || " <" || TempFile
success = DoCommand(DownloadCommand)
/* If the user wants filenotes added, here's where we do it! */
if (AddFileNotes = YES) then call AddFileNotes(LocalRecentFile, TempFile, DownloadDir, FileNoteFormatString, GadMgetDirLength, GadMgetFileLength, GadmgetCommLength)
/* Now delete the Temporary file */
DeleteCommand = "delete " || TempFile || " QUIET"
success = DoCommand(DeleteCommand)
say "AutoFTP script terminating."
exit
/**************************************************************************/
/* ------------------------ BEGIN PROCEDURES ---------------------------- */
/**************************************************************************/
/* Executes an AmigaDos shell command for us, giving output if called for. */
DoCommand: procedure expose Debug
parse arg thiscommand
if (Debug == TRUE) then say "EXECUTING: [" || thiscommand || "]"
address COMMAND thiscommand
return 1
/* Loads in the given RECENT file and CommandFile, and if a filename
is both in the CommandFile and the ScanDir, sets its FileNote
to the comment given in the RECENT file. Any string in the
FormatString will be prepended to the the FileNote, with the keyword
tokens listed below replaced by what they represent. */
AddFileNotes: procedure expose Debug
parse arg RecentFile, CommandFile, ScanDir, NoteExpr, DirLen, FileLen, CommLen
/* Things to replace */
DirToken = "$GADMGETDIR"
FileToken = "$GADMGETFILE"
CommToken = "$GADMGETCOMMENT"
/* Keep for later, if we need to replace $DIR */
OriginalPreamble = NoteExpr
/* First thing we must do: parse the CommandFile */
if ~open(CommandInput, CommandFile, 'R') then do
say "AddFileNotes: Couldn't open temp file ["|| CommandFile|| "]!"
return 0
end
/* Now go through, and get a database of names and dirs */
RequestedFileIndex = 0
do until eof(CommandInput)
ThisLine = readln(CommandInput)
RequestedFileIndex = ParseGetLine(ThisLine, RequestedFileIndex)
end
call close(CommandInput)
/* Next thing we must do: read the RecentFile */
if ~open(RecentInput, RecentFile, 'R') then do
say "AddFileNotes: Couldn't open temp file ["|| RecentFile|| "]!"
return 0
end
/* Now go through, and get a database of names and dirs */
RecentFileIndex = 0
do until eof(RecentInput)
ThisLine = readln(RecentInput)
/* Get data from line */
parse var ThisLine ThisName ThisDir Comment
Comment = ExtractComment(Comment)
/* Scan thru all of our entries, looking for a match */
do TempReqIndex = 0 to (RequestedFileIndex-1)
if ((Files.TempReqIndex.File == ThisName)&(Files.TempReqIndex.Dir == ThisDir)) then Files.TempReqIndex.Comm = Comment
end
end
call close(RecentInput)
/* Now any and all comments should be filled out... so do the SetFileNote thingy */
do TempReqIndex = 0 to (RequestedFileIndex-1)
Note = NoteExpr
Dirx = Files.TempReqIndex.Dir
Filex = Files.TempReqIndex.File
Commx = Files.TempReqIndex.Comm
/* pad/cut as necessary */
if (DirLen > 0) then Dirx = substr(Dirx, 1, DirLen)
if (FileLen > 0) then Filex = substr(Filex, 1, FileLen)
if (CommLen > 0) then Commx = substr(Commx, 1, CommLen)
/* First, make up the filenote string */
do while (Index(Note,DirToken) > 0)
Note = SubstituteString(Note,DirToken,Dirx)
end
do while (Index(Note,FileToken) > 0)
Note = SubstituteString(Note,FileToken,Filex)
end
do while (Index(Note,CommToken) > 0)
Note = SubstituteString(Note,CommToken,Commx)
end
/* Now set it */
if (exists(Files.TempReqIndex.File) == 1) then call DoCommand("FileNote " || Files.TempReqIndex.File || ' "' || Note || '"')
else say "SetFileNote: Warning, couldn't find file [" || Files.TempReqIndex.File || "]"
end
return 1
/* Replaces the first ReplaceMe with WithMe in the string Line */
SubstituteString: procedure expose Debug
parse arg Line, ReplaceMe, WithMe
/* Sanity check--avoid infinite loops */
if (Index(WithMe,ReplaceMe) > 0) then return Line
StartOfVictim = Index(Line, ReplaceMe)
LengthOfVictim = length(ReplaceMe)
Line = DelStr(Line, StartOfVictim, LengthOfVictim)
Line = Insert(WithMe, Line, StartOfVictim-1)
return Line
/* Removes the size info and the "+" from the remainder of the line */
ExtractComment: procedure expose Debug
parse arg Line
Line = Strip(Line)
/* Basically, if the first word has a '+', return starting
after the +, else return starting with the second word */
FirstWord = left(Line,WordLength(Line,1))
PlusPos = Index(FirstWord,"+")
if (PlusPos > 0) then Line = right(Line,length(Line)-PlusPos)
else Line = right(Line,length(Line)-WordIndex(Line,2))
return Line
/* Parses one line of the Command file. Returns the new index to use
for next time (i.e. the next "fresh" one. */
ParseGetLine: procedure expose Debug Files.
parse arg Line, FIndex
do while (length(Line) > 0)
/* Remove any leading or trailing spaces */
Line = Strip(Line)
/* Length of the first word in the line */
FirstWordLength = WordLength(Line, 1)
/* Get the first "word" in the line */
NextWord = left(Line,FirstWordLength)
/* Remove it from the line */
Line = right(Line, Length(Line) - FirstWordLength)
/* Assume that if it's a file, it will have '/' separator */
LastSlash = LastPos("/",NextWord)
if ((LastSlash > 0)&(NextWord ~= "../..")) then do
Files.FIndex.Dir = left(NextWord,LastSlash-1)
Files.FIndex.File = right(NextWord,FirstWordLength-LastSlash)
FIndex = FIndex + 1
end
end
return FIndex
/* Looks at the current RECENT and the new one, and merges them so that:
1) All currently selected lines in the current RECENT are in the
output file's selected section
2) All currently selected lines in the current RECENT are NOT in
the output file's selectable section. */
SmartCopy: procedure expose Debug DoPruning
parse arg OldRecent, NewRecent, OutFile .
if (Debug == TRUE) then say "Merging " || OldRecent || " and " || NewRecent || " into " || Outfile
/* This needs to be accurate! */
BOOKMARK = "| --- GadMGet: Begin Selected Files --- "
/* Load OldRecent into OldRecentSel */
OSelIndex = 0
OKeepInput = 0
if ~open(OldInput, OldRecent, 'R') then return 0
do until eof(OldInput)
ThisLine = readln(OldInput)
if (ThisLine == BOOKMARK) then do
OKeepInput = 1
end
else do
if (OKeepInput == 1) then do
OldRecentSel.OSelIndex = ThisLine
OSelIndex = OSelIndex + 1
end
end
end
call close(OldInput)
if (OSelIndex == 0) then do
/* No selected lines to save. Might as well take the short cut */
CopyCommand = "copy " || NewRecent || " " || OutFile
success = DoCommand(CopyCommand)
return 1
end
/* Load NewRecent into NewRecent.Reg. Presumably since we just
downloaded this from Aminet, there won't be a NewRecent.Sel ! */
NewRecentRegIndex = 0
if ~open(NewInput, NewRecent, 'R') then return 0
if ~open(NewOutput,OutFile, 'W') then do
call close(NewInput)
return 0
end
do until eof(NewInput)
ThisLine = readln(NewInput)
CheckIndex = 0
OKToPrint = 1
if (DoPruning == YES) then do
do while ((CheckIndex < OSelIndex)&(OKToPrint = 1))
if (OldRecentSel.CheckIndex == ThisLine) then OKToPrint = 0
CheckIndex = CheckIndex + 1
end
end
if (OKToPrint == 1) then call writeln(NewOutput,ThisLine)
end
call close(NewInput)
/* Now write the bookmark, and our selected files */
call writeln(NewOutput,BOOKMARK)
WriteIndex = 0
do while (WriteIndex < OSelIndex)
call writeln(NewOutput,OldRecentSel.WriteIndex)
WriteIndex = WriteIndex + 1
end
call close(NewOutput)
return 1
/**************************************************************************/
/* ------------------------ END AUTOFTP SCRIPT CODE --------------------- */
/**************************************************************************/